<?php


class PhotoManager {
    public function __construct(Mypdo $db){
        $this->db= $db;
    }

    public function add(Photo $photo){
        $req = $this->db->prepare('INSERT INTO photo ( id_photo, id_loc, lien_photo, descp_photo ) VALUES
                  ( :id_photo, :id_loc, :lien_photo, :descp_photo )');
        $req->bindValue(':id_photo', $photo->getIdPhoto(), PDO::PARAM_INT);
        $req->bindValue(':id_loc', $photo->getIdLoc(), PDO::PARAM_INT);
        $req->bindValue(':lien_photo', $photo->getLienPhoto(), PDO::PARAM_STR);
        $req->bindValue(':descp_photo', $photo->getDescpPhoto(), PDO::PARAM_INT);
        $req->execute();

    }


    public function getList(){
        $listePhotos = array();
        $sql = 'SELECT * FROM photo ORDER BY id_loc';
        $req = $this->db->query($sql);
        while ($photo = $req->fetch(PDO::FETCH_OBJ)){
            $listePhotos[] = new Photo($photo);
        }
        $req->closeCursor();
        return $listePhotos;

    }


    public function getListByIdLoc($id_loc){
        $listePhotos = array();
        $sql = 'SELECT * FROM photo WHERE id_loc = :id_loc';
        $req = $this->db->prepare($sql);
        $req->bindValue(':id_loc', $id_loc, PDO::PARAM_INT);

        $req->execute();
        while ($photo = $req->fetch(PDO::FETCH_OBJ)){
            $listePhotos[] = new Photo($photo);
        }
        $req->closeCursor();
        return $listePhotos;

    }
    public function getListByIdPropri($id_propri){
        $listePhotos = array();
        $sql = 'SELECT * FROM photo p
        INNER JOIN location l ON p.id_loc=l.id_loc
        INNER JOIN immeuble i ON i.id_immeu = l.id_immeu
        WHERE i.id_propri = :id_propri';
        $req = $this->db->prepare($sql);
        $req->bindValue(':id_propri', $id_propri, PDO::PARAM_INT);

        $req->execute();
        while ($photo = $req->fetch(PDO::FETCH_OBJ)){
            $listePhotos[] = new Photo($photo);
        }
        $req->closeCursor();
        return $listePhotos;

    }
    public function getListByIdImmeu($id_immeu){
        $listePhotos = array();
        $sql = 'SELECT * FROM photo p
        INNER JOIN location l ON p.id_loc=l.id_loc
        WHERE l.id_immeu = :id_immeu';
        $req = $this->db->prepare($sql);
        $req->bindValue(':id_immeu', $id_immeu, PDO::PARAM_INT);

        $req->execute();
        while ($photo = $req->fetch(PDO::FETCH_OBJ)){
            $listePhotos[] = new Photo($photo);
        }
        $req->closeCursor();
        return $listePhotos;

    }

    public function getNbPhoto($id_loc){
        $sql = 'SELECT MAX(id_photo) AS nb FROM photo WHERE id_loc = :id_loc';
        $req = $this->db->prepare($sql);
        $req->bindValue(':id_loc', $id_loc, PDO::PARAM_INT);


        $req->execute();
        return $req->fetch(PDO::FETCH_OBJ)->nb;
    }


    public function  delPhotoByIdPropri($id_propri){

        $liste =$this->getListByIdPropri($id_propri);
        foreach ($liste as $value) {
            unlink('image/upload/'.$value->getLienPhoto());
        }

        $sql = 'DELETE FROM immoloc.photo WHERE id_loc IN
        (SELECT l.id_loc FROM  location l
        INNER JOIN immeuble i ON l.id_immeu = i.id_immeu
        WHERE id_propri = :id_propri)';
        $req = $this->db->prepare($sql);
        $req->bindValue(':id_propri', $id_propri, PDO::PARAM_INT);

        $req->execute();

    }

    public function  delPhotoByIdImmeu($id_immeu){

        $liste =$this->getListByIdImmeu($id_immeu);
        foreach ($liste as $value) {
            unlink('image/upload/'.$value->getLienPhoto());
        }

        $sql = 'DELETE FROM immoloc.photo WHERE id_loc IN
        (SELECT id_loc FROM location
        WHERE id_immeu = :id_immeu)';
        $req = $this->db->prepare($sql);
        $req->bindValue(':id_immeu', $id_immeu, PDO::PARAM_INT);

        $req->execute();

    }

    public function delPhotoByIdLoc($id_loc)
    {
        $liste =$this->getListByIdLoc($id_loc);
        foreach ($liste as $value) {
            unlink('image/upload/'.$value->getLienPhoto());
        }

        $sql = 'DELETE FROM immoloc.photo
        WHERE id_loc = :id_loc';
        $req = $this->db->prepare($sql);
        $req->bindValue(':id_loc', $id_loc, PDO::PARAM_INT);

        $req->execute();
    }

    public function delPhoto($id_loc, $id_photo,$rm = true)
    {
        $photo = $this->getPhoto($id_loc, $id_photo);
        if($rm){
            unlink('image/upload/'.$photo->getLienPhoto());
        }
        $sql = 'DELETE FROM immoloc.photo WHERE id_loc = :id_loc AND id_photo = :id_photo';
        $req = $this->db->prepare($sql);
        $req->bindValue(':id_loc', $id_loc, PDO::PARAM_INT);
        $req->bindValue(':id_photo', $id_photo, PDO::PARAM_INT);

        $req->execute();
    }

    public function getPhoto($id_loc, $id_photo){
        $sql = 'SELECT * FROM photo WHERE id_loc = :id_loc AND id_photo = :id_photo';
        $req = $this->db->prepare($sql);
        $req->bindValue(':id_loc', $id_loc, PDO::PARAM_INT);
        $req->bindValue(':id_photo', $id_photo, PDO::PARAM_INT);
        $req->execute();
        return new Photo($req->fetch(PDO::FETCH_ASSOC));
    }

    public function getPremierPhotoLoc($id_loc)
    {
        $sql = 'SELECT * FROM photo WHERE id_loc = :id_loc AND id_photo = 1';
        $req = $this->db->prepare($sql);
        $req->bindValue(':id_loc', $id_loc, PDO::PARAM_INT);
        $req->execute();
        $photo = $req->fetch(PDO::FETCH_ASSOC);
        return new Photo($photo);
    }



    public function updatePremierePhoto($id_loc){
        $sql = 'SELECT * FROM photo WHERE id_loc = :id_loc ORDER BY id_photo';
        $req = $this->db->prepare($sql);
        $req->bindValue(':id_loc', $id_loc, PDO::PARAM_INT);
        $req->execute();
        $photo = new Photo($req->fetch(PDO::FETCH_ASSOC));

        if(!$photo->isEmpty()){
        $supprloc = $photo->getIdLoc();
        $supprpho = $photo->getIdPhoto();
        $file = new SplFileInfo($photo->getLienPhoto());
        $photo->setIdPhoto(1);
        rename('image/upload/'.$photo->getLienPhoto(),'image/upload/'.$photo->getIdLoc().'-'.$photo->getIdPhoto().'.'.$file->getExtension());
        $photo->setLienPhoto($photo->getIdLoc().'-'.$photo->getIdPhoto().'.'.$file->getExtension());
        $this->delPhoto($supprloc,$supprpho);
        $this->add($photo);
        }

    }

} 